/****************************************************************************
 **
 ** Copyright (C) 2004-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License versions 2.0 or 3.0 as published by the Free Software
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
 ** included in the packaging of this file.  Alternatively you may (at
 ** your option) use any later version of the GNU General Public
 ** License if such license has been publicly approved by Trolltech ASA
 ** (or its successors, if any) and the KDE Free Qt Foundation. In
 ** addition, as a special exception, Trolltech gives you certain
 ** additional rights. These rights are described in the Trolltech GPL
 ** Exception version 1.2, which can be found at
 ** http://www.trolltech.com/products/qt/gplexception/ and in the file
 ** GPL_EXCEPTION.txt in this package.
 **
 ** Please review the following information to ensure GNU General
 ** Public Licensing requirements will be met:
 ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
 ** you are unsure which license is appropriate for your use, please
 ** review the following information:
 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
 ** or contact the sales department at sales@trolltech.com.
 **
 ** In addition, as a special exception, Trolltech, as the sole
 ** copyright holder for Qt Designer, grants users of the Qt/Eclipse
 ** Integration plug-in the right for the Qt/Eclipse Integration to
 ** link to functionality provided by Qt Designer and its related
 ** libraries.
 **
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
 ** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

/* This file originally comes from Qt Tetrix example code.  I don't claim this
 * as my own work.  I mearly modified it to meet the project specifications */

#ifndef TETRIXBOARD_H
#define TETRIXBOARD_H

#include <QBasicTimer>
#include <QFrame>
#include <QPointer>

#include "tetrixpiece.h"
#include "piecegenerator.h"
#include "keyconfig.h"

class QLabel;

/// Frame in which all tetrix pieces are displayed
class TetrixBoard : public QFrame
{
    Q_OBJECT

public:
    /// Key command enumeration
    typedef enum
    {
        RotateKey = 0, ///!< Rotates piece clockwise 90 degrees
        LeftKey, ///!< Moves piece left
        RightKey, ///!< Moves piece right
        FallKey, ///!< Soft drop the piece
        Numkeys ///!< Number of key commands
    } Keys;

    /*!
     * @param pieceFileName file to pull pieces from, if left blank random
     *                      pieces will be generatred
     * @param parent QWidget
     */
    TetrixBoard(const char *pieceFileName = 0, QWidget *parent = 0);

    /// Informs TetrixBoard what QLabel to use as the next piece label
    /*!
     * @param label QLabel to use as next piece label
     */
    void setNextPieceLabel(QLabel *label);
    /// Returns the recommended size for the TetrixBoard
    QSize sizeHint() const;
    /// Returns the minimum size for the TetrixBoard
    QSize minimumSizeHint() const;
    /// Sets specific key command
    /*!
     * @param key key to set
     * @param value to set key to
     */
    void setKey(Keys key, KeyConfig::KeyCode value);

public slots:
    /// Slot to start a new game
    void start();
    /// Slot to pause the current game
    void pause();

signals:
    /// Emitted when a line is removed
    void linesRemovedChanged(int numLines);
    /// Emitted when the total number of pieces dropped changes
    void totalPiecesChanged(int numPieces);
    /// Emitted when the total number of pieces dropped changes.  Indicates the
    //  total block count of all pieces
    void totalBlocksChanged(int numBlocks);
    /// Emitted when a 4 block piece is dropped
    void piece4BlocksChanged(int numPieces);
    /// Emitted when a 5 block piece is dropped
    void piece5BlocksChanged(int numPieces);
    /// Emitted when a 6 block piece is dropped
    void piece6BlocksChanged(int numPieces);
    /// Emitted when a 7 block piece is dropped
    void piece7BlocksChanged(int numPieces);

protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void timerEvent(QTimerEvent *event);

private:
    enum { BoardWidth = 20, BoardHeight = 30 };

    int &shapeAt(int x, int y) { return board[(y * BoardWidth) + x]; }
    int squareWidth() { return contentsRect().width() / BoardWidth; }
    int squareHeight() { return contentsRect().height() / BoardHeight; }
    void clearBoard();
    void dropDown();
    void oneLineDown();
    void pieceDropped();
    void removeFullLines();
    void newPiece();
    void showNextPiece();
    bool tryMove(const TetrixPiece &newPiece, int newX, int newY);
    void drawSquare(QPainter &painter, int x, int y, int numBlocks);

    QBasicTimer timer;
    QPointer<QLabel> nextPieceLabel;
    bool isStarted;
    bool isPaused;
    bool isGameOver;
    bool isWaitingAfterLine;
    TetrixPiece curPiece;
    TetrixPiece nextPiece;
    int curX;
    int curY;
    int numLinesRemoved;
    int totalPieces;
    int totalBlocks;
    int piece4Blocks;
    int piece5Blocks;
    int piece6Blocks;
    int piece7Blocks;
    KeyConfig::KeyCode keys[Numkeys];
    int board[BoardWidth * BoardHeight];
    PieceGenerator *pieceGen;
    static int timeoutTime;
};

#endif
